package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.datasource.entities.AccountHeadVo;
import com.jsh.erp.datasource.entities.CheckVo;
import com.jsh.erp.datasource.vo.AccountItemVo4List;
import com.jsh.erp.service.accountItem.AccountItemService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ji sheng hua 752*718*920
 */
@RestController
@RequestMapping(value = "/accountItem")
@Api(tags = {"财务明细"})
public class AccountItemController {
    private Logger logger = LoggerFactory.getLogger(AccountItemController.class);

    @Resource
    private AccountItemService accountItemService;

    @Resource
    private RedisService redisService;

    @GetMapping(value = "/getDetailList")
    @ApiOperation(value = "明细列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AccountItemVo4List.class)
    })
    public BaseResponseInfo getDetailList(@RequestParam("headerId") String headerId,
                                          HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        try {
            List<AccountItemVo4List> dataList = new ArrayList<>();
            if(!StringUtil.isEmpty(headerId)) {
                dataList = accountItemService.getDetailList(headerId);
            }
            JSONObject outer = new JSONObject();
            outer.put("total", dataList.size());
            //存放数据json数组
            JSONArray dataArray = new JSONArray();
            if (dataList.size() > 0) {
                for (AccountItemVo4List ai : dataList) {
                    JSONObject item = new JSONObject();
                    item.put("accountId", ai.getAccountId());
                    item.put("accountName", ai.getAccountName());
                    item.put("inOutItemId", ai.getInOutItemId());
                    item.put("inOutItemName", ai.getInOutItemName());
                    item.put("billNumber", ai.getBillNumber());
                    item.put("needDebt", ai.getNeedDebt());
                    item.put("finishDebt", ai.getFinishDebt());
                    BigDecimal eachAmount = ai.getEachAmount();
                    item.put("eachAmount",  eachAmount);
                    item.put("remark", ai.getRemark());

                    item.put("transpotNo", ai.getTranspotNo());
                    item.put("transpotName", ai.getTranspotName());
                    item.put("packSize", ai.getPackSize());
                    item.put("gtin", ai.getGtin());
                    item.put("uom", ai.getUom());
                    item.put("packsOrdered", ai.getPacksOrdered());
                    item.put("packsSupplied", ai.getPacksSupplied());
                    item.put("unitPrice", ai.getUnitPrice());
                    item.put("amount", ai.getAmount());
                    item.put("discountAmount", ai.getDiscountAmount());
                    item.put("paymentType", ai.getPaymentType());
                    item.put("receiveTime", ai.getReceiveTime());
                    item.put("depotId", ai.getDepotId());
                    item.put("invoiceNo", ai.getInvoiceNo());
                    item.put("depotName", ai.getDepotName());
                    item.put("isExchange", ai.getIsExchange());
                    item.put("custom1",ai.getCustom1());
                    item.put("custom2",ai.getCustom2());
                    item.put("custom3",ai.getCustom3());
                    item.put("sourceId",ai.getCustom4());
                    item.put("custom4",ai.getCustom4());
                    item.put("custom5",ai.getCustom5());
                    item.put("custom6",ai.getCustom6());
                    item.put("custom7",ai.getCustom7());
                    item.put("custom8",ai.getCustom8());
                    item.put("custom9",ai.getCustom9());
                    item.put("custom10",ai.getCustom10());
                    item.put("custom11",ai.getCustom11());
                    item.put("custom12",ai.getCustom12());
                    item.put("custom13",ai.getCustom13());
                    item.put("custom14",ai.getCustom14());
                    item.put("custom15",ai.getCustom15());
                    dataArray.add(item);
                }
            }
            outer.put("rows", dataArray);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    @GetMapping(value = "/checkList")
    @ApiOperation(value = "要货申请预警")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AccountHeadVo.class)
    })
    public BaseResponseInfo checkList(@RequestParam @ApiParam("客户id") String organId,
                                                     HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        Map<String, Object> map = new HashMap<String, Object>();
        List<CheckVo> vo = accountItemService.checkList(organId);
        map.put("rows", vo);
        res.data = map;
        res.code = 200;
        return res;
    }
}
